#define DEBUG
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Xml;

namespace ICSharpCode.TextEditor.Document
{
	public class HighlightColor
	{
		private Color color;

		private Color backgroundcolor = Color.WhiteSmoke;

		private bool bold = false;

		private bool italic = false;

		private bool hasForeground = false;

		private bool hasBackground = false;

		public bool HasForeground => this.hasForeground;

		public bool HasBackground => this.hasBackground;

		public bool Bold => this.bold;

		public bool Italic => this.italic;

		public Color BackgroundColor => this.backgroundcolor;

		public Color Color => this.color;

		public Font GetFont(FontContainer fontContainer)
		{
			if (this.Bold)
			{
				return this.Italic ? fontContainer.BoldItalicFont : fontContainer.BoldFont;
			}
			return this.Italic ? fontContainer.ItalicFont : fontContainer.RegularFont;
		}

		private Color ParseColorString(string colorName)
		{
			string[] array = colorName.Split('*');
			PropertyInfo property = typeof(SystemColors).GetProperty(array[0], BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
			Color result = (Color)property.GetValue(null, null);
			if (array.Length == 2)
			{
				double num = double.Parse(array[1]) / 100.0;
				result = Color.FromArgb((int)((double)(int)result.R * num), (int)((double)(int)result.G * num), (int)((double)(int)result.B * num));
			}
			return result;
		}

		public HighlightColor(XmlElement el)
		{
			Debug.Assert(el != null, "ICSharpCode.TextEditor.Document.SyntaxColor(XmlElement el) : el == null");
			if (el.Attributes["bold"] != null)
			{
				this.bold = bool.Parse(el.Attributes["bold"]!.InnerText);
			}
			if (el.Attributes["italic"] != null)
			{
				this.italic = bool.Parse(el.Attributes["italic"]!.InnerText);
			}
			if (el.Attributes["color"] != null)
			{
				string innerText = el.Attributes["color"]!.InnerText;
				if (innerText[0] == '#')
				{
					this.color = HighlightColor.ParseColor(innerText);
				}
				else if (innerText.StartsWith("SystemColors."))
				{
					this.color = this.ParseColorString(innerText.Substring("SystemColors.".Length));
				}
				else
				{
					this.color = (Color)this.Color.GetType().InvokeMember(innerText, BindingFlags.GetProperty, null, this.Color, new object[0]);
				}
				this.hasForeground = true;
			}
			else
			{
				this.color = Color.Transparent;
			}
			if (el.Attributes["bgcolor"] != null)
			{
				string innerText2 = el.Attributes["bgcolor"]!.InnerText;
				if (innerText2[0] == '#')
				{
					this.backgroundcolor = HighlightColor.ParseColor(innerText2);
				}
				else if (innerText2.StartsWith("SystemColors."))
				{
					this.backgroundcolor = this.ParseColorString(innerText2.Substring("SystemColors.".Length));
				}
				else
				{
					this.backgroundcolor = (Color)this.Color.GetType().InvokeMember(innerText2, BindingFlags.GetProperty, null, this.Color, new object[0]);
				}
				this.hasBackground = true;
			}
		}

		public HighlightColor(XmlElement el, HighlightColor defaultColor)
		{
			Debug.Assert(el != null, "ICSharpCode.TextEditor.Document.SyntaxColor(XmlElement el) : el == null");
			if (el.Attributes["bold"] != null)
			{
				this.bold = bool.Parse(el.Attributes["bold"]!.InnerText);
			}
			else
			{
				this.bold = defaultColor.Bold;
			}
			if (el.Attributes["italic"] != null)
			{
				this.italic = bool.Parse(el.Attributes["italic"]!.InnerText);
			}
			else
			{
				this.italic = defaultColor.Italic;
			}
			if (el.Attributes["color"] != null)
			{
				string innerText = el.Attributes["color"]!.InnerText;
				if (innerText[0] == '#')
				{
					this.color = HighlightColor.ParseColor(innerText);
				}
				else if (innerText.StartsWith("SystemColors."))
				{
					this.color = this.ParseColorString(innerText.Substring("SystemColors.".Length));
				}
				else
				{
					this.color = (Color)this.Color.GetType().InvokeMember(innerText, BindingFlags.GetProperty, null, this.Color, new object[0]);
				}
				this.hasForeground = true;
			}
			else
			{
				this.color = defaultColor.color;
			}
			if (el.Attributes["bgcolor"] != null)
			{
				string innerText2 = el.Attributes["bgcolor"]!.InnerText;
				if (innerText2[0] == '#')
				{
					this.backgroundcolor = HighlightColor.ParseColor(innerText2);
				}
				else if (innerText2.StartsWith("SystemColors."))
				{
					this.backgroundcolor = this.ParseColorString(innerText2.Substring("SystemColors.".Length));
				}
				else
				{
					this.backgroundcolor = (Color)this.Color.GetType().InvokeMember(innerText2, BindingFlags.GetProperty, null, this.Color, new object[0]);
				}
				this.hasBackground = true;
			}
			else
			{
				this.backgroundcolor = defaultColor.BackgroundColor;
			}
		}

		public HighlightColor(Color color, bool bold, bool italic)
		{
			this.hasForeground = true;
			this.color = color;
			this.bold = bold;
			this.italic = italic;
		}

		public HighlightColor(Color color, Color backgroundcolor, bool bold, bool italic)
		{
			this.hasForeground = true;
			this.hasBackground = true;
			this.color = color;
			this.backgroundcolor = backgroundcolor;
			this.bold = bold;
			this.italic = italic;
		}

		public HighlightColor(string systemColor, string systemBackgroundColor, bool bold, bool italic)
		{
			this.hasForeground = true;
			this.hasBackground = true;
			this.color = this.ParseColorString(systemColor);
			this.backgroundcolor = this.ParseColorString(systemBackgroundColor);
			this.bold = bold;
			this.italic = italic;
		}

		public HighlightColor(string systemColor, bool bold, bool italic)
		{
			this.hasForeground = true;
			this.color = this.ParseColorString(systemColor);
			this.bold = bold;
			this.italic = italic;
		}

		private static Color ParseColor(string c)
		{
			int alpha = 255;
			int num = 0;
			if (c.Length > 7)
			{
				num = 2;
				alpha = int.Parse(c.Substring(1, 2), NumberStyles.HexNumber);
			}
			int red = int.Parse(c.Substring(1 + num, 2), NumberStyles.HexNumber);
			int green = int.Parse(c.Substring(3 + num, 2), NumberStyles.HexNumber);
			int blue = int.Parse(c.Substring(5 + num, 2), NumberStyles.HexNumber);
			return Color.FromArgb(alpha, red, green, blue);
		}

		public override string ToString()
		{
			return "[HighlightColor: Bold = " + this.Bold + ", Italic = " + this.Italic + ", Color = " + this.Color.ToString() + ", BackgroundColor = " + this.BackgroundColor.ToString() + "]";
		}
	}
}
